import UtilFuncs from "../../common/tools/utilfuncs";
var funcs = UtilFuncs.UtilFuncs;
import {CoordTRFUtil}  from "../../common/tools/coordtool";

import arrowManager from "./arrowManager";

var ArrowUtil = {}

ArrowUtil.dis = function (arrowPt, lnvct, map, mapinfo, color, arrowType) {
    if (arrowType == "none") {
        return;
    }
    let arrowMng = new arrowManager.LGXArrowManager();
    let arrowInfo = arrowMng.getArrowInfo(arrowType);
    if (!arrowInfo) {
        return;
    }
    if (arrowInfo.type == "triangle") {
        this.disTriangleArrow(arrowPt, lnvct, map, mapinfo, color, arrowInfo);
    }
    else if (arrowInfo.type == "diamond") {
        this.disDiamondArrow(arrowPt, lnvct, map, mapinfo, color, arrowInfo);
    }
    else if (arrowInfo.type == "cad") {
        this.disCADArrow(arrowPt, lnvct, map, mapinfo, color, arrowInfo);
    }
}

ArrowUtil.disCADArrow = function (arrowPt, lnvct, map, mapinfo, color, arrowInfo) {
    let close = arrowInfo.close;
    let fill = arrowInfo.fill;
    let angle = arrowInfo.angle / 2;
    var arrowSPt = CoordTRFUtil.world2Screen(arrowPt, mapinfo);
    let scaling = mapinfo.getScaling();
    var gap = arrowInfo.size;

    var rotLeftVct = lnvct.cloneCopy();
    rotLeftVct.rotate(angle);
    var x = arrowPt.x + gap * rotLeftVct.m;
    var y = arrowPt.y + gap * rotLeftVct.n;
    var pt = { x, y };
    var leftPt = CoordTRFUtil.world2Screen(pt, mapinfo);

    var rotRightVct = lnvct.cloneCopy();
    rotRightVct.rotate(-angle);
    x = arrowPt.x + gap * rotRightVct.m;
    y = arrowPt.y + gap * rotRightVct.n;
    pt = { x, y };
    var rightPt = CoordTRFUtil.world2Screen(pt, mapinfo);

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = color;

    if (close) {
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(leftPt.x, leftPt.y);
        map.lineTo(rightPt.x, rightPt.y);
        map.closePath();
        map.stroke();
    }
    else {
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(leftPt.x, leftPt.y);
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(rightPt.x, rightPt.y);
        map.stroke();
        map.closePath();
    }
    if (fill) {
        map.fillStyle = color;
        map.fill();
    }
}

ArrowUtil.disDiamondArrow = function (arrowPt, lnvct, map, mapinfo, color, arrowInfo) {
    let fill = arrowInfo.fill;
    let angle = arrowInfo.angle / 2;
    var arrowSPt = CoordTRFUtil.world2Screen(arrowPt, mapinfo);
    let scaling = mapinfo.getScaling();
    var gap = arrowInfo.size / scaling;

    var rotLeftVct = lnvct.cloneCopy();
    rotLeftVct.rotate(angle);
    var x = arrowPt.x + gap * rotLeftVct.m;
    var y = arrowPt.y + gap * rotLeftVct.n;
    let leftWorldPt = { x, y };
    var leftPt = CoordTRFUtil.world2Screen(leftWorldPt, mapinfo);

    var rotRightVct = lnvct.cloneCopy();
    rotRightVct.rotate(-angle);
    x = arrowPt.x + gap * rotRightVct.m;
    y = arrowPt.y + gap * rotRightVct.n;
    let rightWorldPt = { x, y };
    var rightPt = CoordTRFUtil.world2Screen(rightWorldPt, mapinfo);

    let arrowPtMirror2Axis = funcs.mirrorPoint(arrowPt, leftWorldPt, rightWorldPt);
    var screenArrowPtMirror2Axis = CoordTRFUtil.world2Screen(arrowPtMirror2Axis, mapinfo);

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = color;

    map.moveTo(arrowSPt.x, arrowSPt.y);
    map.lineTo(leftPt.x, leftPt.y);
    map.lineTo(screenArrowPtMirror2Axis.x, screenArrowPtMirror2Axis.y);
    map.lineTo(rightPt.x, rightPt.y);
    map.closePath();
    map.stroke();

    if (fill) {
        map.fillStyle = color;
        map.fill();
    }
}

ArrowUtil.disTriangleArrow = function (arrowPt, lnvct, map, mapinfo, color, arrowInfo) {
    let close = arrowInfo.close;
    let fill = arrowInfo.fill;
    let angle = arrowInfo.angle / 2;
    var arrowSPt = CoordTRFUtil.world2Screen(arrowPt, mapinfo);
    let scaling = mapinfo.getScaling();
    var gap = arrowInfo.size / scaling;

    var rotLeftVct = lnvct.cloneCopy();
    rotLeftVct.rotate(angle);
    var x = arrowPt.x + gap * rotLeftVct.m;
    var y = arrowPt.y + gap * rotLeftVct.n;
    var pt = { x, y };
    var leftPt = CoordTRFUtil.world2Screen(pt, mapinfo);

    var rotRightVct = lnvct.cloneCopy();
    rotRightVct.rotate(-angle);
    x = arrowPt.x + gap * rotRightVct.m;
    y = arrowPt.y + gap * rotRightVct.n;
    pt = { x, y };
    var rightPt = CoordTRFUtil.world2Screen(pt, mapinfo);

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = color;

    if (close) {
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(leftPt.x, leftPt.y);
        map.lineTo(rightPt.x, rightPt.y);
        map.closePath();
        map.stroke();
    }
    else {
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(leftPt.x, leftPt.y);
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(rightPt.x, rightPt.y);
        map.stroke();
        map.closePath();
    }
    if (fill) {
        map.fillStyle = color;
        map.fill();
    }
}

export default { ArrowUtil }
